<template>
  <div>
    <h1>登录</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="账号" style="width: 50%;">
        <el-input v-model="form.username"></el-input>
      </el-form-item>

      <el-form-item label="密码" style="width: 50%;">
        <el-input v-model="form.password" show-password></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="toLogin">登录</el-button>
        <el-button @click="toRegister()">注册</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
      }
    }
  },
  methods: {
    toLogin() {
      this.axios.post("/user/login", this.form).then(res => {
        if (res.data) {
          localStorage.setItem("user", JSON.stringify(res.data));
          this.$message("登陆成功");
          this.$router.push("/index");
        } else {
          this.$message("对不起,账号或密码错误");
        }
      })
    },
    toRegister(){
          this.$router.push("/register");
    }
  }
}
</script>
<style>
</style>